package com.lx.core.weight.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lx.core.util.DateUtils;
import com.lx.core.weight.entity.dto.SettlementDTO;
import com.lx.core.weight.entity.dto.WeightPageDTO;
import com.lx.core.weight.entity.vo.SettlementPageList;
import com.lx.core.weight.entity.vo.SettlementVO;
import com.lx.core.weight.entity.vo.WeightPageList;
import com.lx.core.weight.entity.vo.WeightVO;
import com.lx.core.weight.mapper.SettlementMapper;
import com.lx.core.weight.service.SettlementService;
import org.springframework.stereotype.Service;

/**
 * @author lx
 * @date 2025/08/26 16:26
 **/
@Service
public class SettlementServiceImpl implements SettlementService {

    private final SettlementMapper settlementMapper;

    public SettlementServiceImpl(SettlementMapper settlementMapper) {
        this.settlementMapper = settlementMapper;
    }

    @Override
    public SettlementVO page(IPage<SettlementVO> page, SettlementDTO settlementDTO) {
        settlementDTO.setStartTime(DateUtils.formatStartTime(settlementDTO.getStartTime()));
        settlementDTO.setEndTime(DateUtils.formatEndTime(settlementDTO.getEndTime()));
        IPage<SettlementPageList> pageList = settlementMapper.findSettlementPage(page, settlementDTO);
        // 计算业务统计值
        int allTotal = 0;
        int allWeightFee = 0;
        if (!ObjectUtil.isEmpty(pageList) && !ObjectUtil.isEmpty(pageList.getRecords())) {
            for (SettlementPageList record : pageList.getRecords()) {
                allTotal += record.getTotal();
                allWeightFee += record.getWeightFee();
            }
        }
        // 构建返回结果
        SettlementVO result = new SettlementVO();
        result.setRecords(pageList.getRecords());
        result.setTotal(pageList.getTotal());
        result.setSize(pageList.getSize());
        result.setCurrent(pageList.getCurrent());
        result.setPages(pageList.getPages());
        result.setAllTotal(allTotal);
        result.setAllWeightFee(allWeightFee);
        return result;
    }
}
